# -*- coding: utf8 -*-
from human import Human
from player import Player
from enemy import Enemy
class Map(object):
    def __init__(self,tmap=[[]]):
        self.tmap=tmap
    def fly(self,ppl,x,y):
        '''让角色在地图上移动的方法'''
        try:
            if ppl.mround == 2 and self.tmap[x][y]==0:
                    a,b=self.getPosition(ppl)
                    if (abs(x-a)+abs(y-b))>ppl.mov:
                        return False
                    self.tmap[x][y]=ppl
                    self.tmap[a][b]=0
                    ppl.mround = ppl.mround - 2
                    ppl.position=(x,y)
                    return x,y
            print 'you are not able to fly there'
            return False
        except:
            print 'out of array'
            return False
    def getPosition(self,ppl):
        '''获取角色在地图上的坐标'''
        for obj2,i,j in self.filt():
            if obj2 == ppl:
                ppl.position=(i,j)
                return i,j
    def complete(self):
        '''判断英雄是否胜利'''
        for obj2,i,j in self.filt():
            if isinstance(obj2,Enemy):
                if obj2.isOver():
                    return True
                return False
            elif isinstance(obj2,Player):
                if obj2.isOver():
                    return True
                return False
    def mapRoundStart(self,flug='hero'):
        '''回合开始'''
        if flug == 'hero':
            for obj2,i,j in self.filt():
                if isinstance(obj2,Player):
                    obj2.mround=2
        elif flug == 'enemy':
            for obj2,i,j in self.filt():
                if isinstance(obj2,Enemy):
                    obj2.mround=2
    def missionStart(self):
        '''本关开始'''
        for obj2,i,j in self.filt():
            if isinstance(obj2,Human):
                self.getPosition(obj2)
    def filt(self):
        for i,obj in enumerate(self.tmap):
            for j,obj2 in enumerate(obj):
                yield obj2,i,j